<!-- 添加修改公告 -->
<template>
  <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%" @close="close">
    <!-- 添加公告表单 -->
    <el-form :model="noticeModel" label-width="100px">
      <el-form-item label="公告标题">
        <el-input v-model="noticeModel.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="公告内容">
        <div class="editor">
          <quill-editor theme="snow" v-model:content="noticeModel.content" contentType="html">
          </quill-editor>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addnotice(1)">发布</el-button>
        <el-button type="info" @click="addnotice(0)">草稿</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import { addNotice as addNoticeApi, updateNotice } from "@/api/platfrom";
import message from "@/composable/ElMessage";

const visibleDrawer = ref(false);
const title = ref("");

const noticeModel = reactive({
  id: 0,
  title: "",
  content: "",
  status: 0, // 0 草稿 1 发布
});

const open = (t: string, row: any) => {
  title.value = t;
  visibleDrawer.value = true;
  if (row) {
    // Object.assign(noticeModel, row);
    noticeModel.title = row.title;
    noticeModel.content = row.content;
    noticeModel.status = row.status;
    noticeModel.id = row.id;
  }
};

const close = () => {
  visibleDrawer.value = false;
  Object.assign(noticeModel, {
    title: "",
    content: "",
    status: 0,
  });
};

defineExpose({
  open,
  close,
});

const emit = defineEmits(["refresh"]);

const addnotice = async (status: number) => {
  noticeModel.status = status;
  if (title.value == "添加公告") {
    await addNoticeApi(noticeModel).then((res: any) => {
      // console.log(data);
      if (res.code === "200") {
        message.success("添加成功");
        emit("refresh");
        visibleDrawer.value = false;
        close();
      }
    });
  } else if (title.value == "修改公告") {
    console.log(noticeModel);
    await updateNotice(noticeModel).then((res: any) => {
      // console.log(data);
      if (res.code === "200") {
        message.success("修改成功");
        emit("refresh");
        visibleDrawer.value = false;
        close();
      }
    });
  }
};
</script>

<style scoped lang="scss">
.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>
